import math
from numpy import ma
from Tkinter import *
from numpy import deg2rad
from numpy import rad2deg

def createDistHistoPara(dict, minDist, interval, noOfBins):
    distList=[]
    for k, v in dict.items():
        distList.append(v[1])
        
    if minDist=='':
        minDist=min(distList)
        
    if noOfBins=='':
        noOfBins=10.0
        
    if interval=='':        
        interval=round(((max(distList)-float(minDist))/float(noOfBins)), 2)
      
    distInterList=[]
    for i in range(int(noOfBins)+1):
        distInterList.append(round(float(minDist)+(i*float(interval)), 2))
        
    noForIntervalsDict={}
    for i in range(len(distInterList)): 
        j=0
        if i<(len(distInterList)-1): 
            for dist in distList:
                if distInterList[i]<=dist<distInterList[i+1]:
                    j=j+1
            noForIntervalsDict[distInterList[i]]=j
            
        else:
            for dist in distList:
                if dist==distInterList[i]:
                    j=j+1
            noForIntervalsDict[distInterList[i-1]]=noForIntervalsDict[distInterList[i-1]]+j+1 
            noForIntervalsDict[distInterList[i]]=0
        
        
    return noForIntervalsDict

def computeDistMoments(dict):
    distList=[]
    for v in dict.values():
        distList.append(v[1])
        
    momentList=[ma.mean(distList)]
    momentList.append(ma.var(distList))
    
    return momentList
    

def createDirectHistoPara(dict):
    directList=[]
    for k, v in dict.items():
        directList.append(v[0])        
       
    noForIntervalsDirect={}
    
    for j in range(12):
        noForIntervalsDirect[30+30*j]=0
        
    for i in range(len(directList)): 
        for j in range(12):
            if directList[i]<=30+30*j and directList[i]>0+30*j:
                noForIntervalsDirect[30+30*j]=noForIntervalsDirect[30+30*j]+1
                            
        
    return noForIntervalsDirect

def computeDirectMoments(dict):
    directList=[]
    for v in dict.values():
        directList.append(v[0])
        
    sumOfSin=sum([ma.sin(deg2rad(d)) for d in directList])
    sumOfCos=sum([ma.cos(deg2rad(d)) for d in directList])  
    
    directMean=rad2deg(ma.arctan2(sumOfSin, sumOfCos))
    if directMean<0:
        directMean=360+directMean
    
   
    directVariance=1-(ma.sqrt((sumOfSin**2)+(sumOfCos**2))/len(directList))
        
    momentList=[directMean,directVariance]
    
    return momentList

def test():  
    
    root = Tk()    
    cv = Canvas(root,bg = 'white')
    cv.config(height=500, width=500)
    cv.create_arc((200,200 ,300,300),style = PIESLICE,start = 0,extent = 30 )
    cv.create_arc((50,50 ,450,450),style = PIESLICE,start = 30,extent = 30, width=2 )
    cv.pack()
    root.mainloop()

if __name__=='__main__':
    a=createDistHistoPara({'a':[10, 1], 'b':[20,2], 'c':[30,3], 'd':[40,4]}, 1, 1, 4)
    print a
    print len(a)
    b=computeDistMoments({'a':[10, 1], 'b':[20,2], 'c':[30,3], 'd':[40,4]})
    print b
    c=createDirectHistoPara({'a':[10, 1], 'b':[20,2], 'c':[30,3], 'd':[40,4]})
    print c
    d=computeDirectMoments({'a':[290, 1], 'b':[290,2], 'c':[290,3], 'd':[290,4]})
    print d
    
        

    